/*
@author: zwz
@file: 06.实现二叉树.go
@time: 2022/12/7 0007 15:54:43
*/
package main

import "fmt"

//定义结构体
type TreeNode struct {
	value int		//节点值
	left *TreeNode	//左节点
	right *TreeNode	//右节点
}

//插入节点
func Insert(t *TreeNode, value int) *TreeNode {
	if t == nil {
		t = new(TreeNode)
		t.value = value
		return t
	}
	if value < t.value {
		t.left = Insert(t.left, value)
	} else {
		t.right = Insert(t.right, value)
	}
	return t
}

//中序遍历
func inOrder(node *TreeNode) {
	if node != nil {
		inOrder(node.left)
		fmt.Printf("%d", node.value)
		inOrder(node.right)
	}
}

func main() {
	values := []int{3, 4, 2, 5}

	var tree *TreeNode
	for _, value := range values {
		tree = Insert(tree, value)
	}
	inOrder(tree)
}
